/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年4月6日
 */
package org.scauinfo.fpos.service.impl;

import java.io.Serializable;
import java.util.List;

import org.scauinfo.fpos.dao.impl.UserDao;
import org.scauinfo.fpos.service.UserService;
import org.scauinfo.fpos.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 蔡超敏
 * 
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public void saveUser(User user) {
		userDao.save(user);
	}

	@Override
	public void updateUser(User user) {
		userDao.update(user);
	}

	@Override
	public void deleteUser(int id) {
		User user = userDao.get(id);
		user.setUserrole(null);
		userDao.delete(user);
	}

	@Override
	public List<User> findAllUser(int page, int rows, String sort, String order) {
		return userDao.findAll(page, rows, sort, order);
	}

	@Override
	public int getTotal() {
		return userDao.countAll();
	}

	@Override
	public boolean isExist(String userId) {
		boolean flag = false;
		String hql = "from User as p where p.userId= '" + userId + "'";
		if (userDao.find(hql).size() > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public User validateUser(String account, String password) {
		User user;
		String hql = "from User as p where p.account= '" + account + "'";
		List<User> users = userDao.find(hql);
		if (users.size() > 0) {
			user = users.get(0);
			if (user.getPassword().trim().equals(password.trim()))
				return user;
		}
		return null;
	}

	@Override
	public User get(Serializable id) {
		return userDao.get(id);
	}

	/* (non-Javadoc)
	 * @see org.scauinfo.fpos.service.UserService#findAllUser()
	 */
	@Override
	public List<User> findAllUser() {
		return userDao.findAll();
	}
}
